package com.zyguo.thread;

import java.util.ArrayList;


public class ThreadGroup_interrupt {
	public static void main(String[] args) {
		int threadNum = 10;
		final ThreadGroup tgroup = new ThreadGroup("search-threadgroup");
		ArrayList<Thread> tList = new ArrayList<>();
		//定义10个线程
		for( int i = 0; i < threadNum; i++ ){
			Thread t = new Thread( tgroup, new SearchRunnable( 5000 + i*1000) ,"search-thread-" + i);
			tList.add( t );
			t.start();
			System.out.println("start thread = " + t );
		}

		//监控线程的活动的子线程数
		Thread t = new Thread( new Runnable() {
			@Override
			public void run() {
				int activeCount = tgroup.activeCount();
				while ( activeCount > 0 ) {
					System.out.println("activeCount=" + activeCount );
					if( activeCount < 5 ){
						System.out.println("找到了需要的文件，开始终止其他的子线程"   );
						tgroup.interrupt();
						break;
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					activeCount = tgroup.activeCount();
				}
			}
		});
		
		t.start();
		
		
		
		
		
	}
}

